package Classes;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class ListenerChat implements Runnable{
	
	private Integer port = null;
	private String message = null;
	private javax.swing.JTextArea jTexto = null;
	private String name = null;
		
	public ListenerChat(int port, String name, javax.swing.JTextArea conversa) {
		this.port = port;
		this.jTexto = conversa;
		this.name = name;
	}
	
	public static void main(String[] args) {

	/*	Enumeration nis = null;  
		try {  
			nis = NetworkInterface.getNetworkInterfaces();  
		} catch (SocketException e) {  
			e.printStackTrace();  
		}  

		while (nis.hasMoreElements()) {    
			NetworkInterface ni = (NetworkInterface) nis.nextElement();    
			Enumeration ias = ni.getInetAddresses();
			while (ias.hasMoreElements()) {    
				InetAddress ia = (InetAddress) ias.nextElement();  
				if (!ni.getName().equals("lo"))  
					System.out.println(ni.getName()+"  "+ia.getHostAddress());     
			}    
		}*/
	}

	public void run() {
		try {			
			while(true){
				DatagramSocket serverSocket = new DatagramSocket(this.port);
				DatagramPacket DPmsg = new DatagramPacket(new byte[512], 512);
				serverSocket.receive(DPmsg);
				this.message = new String(DPmsg.getData(),DPmsg.getOffset(),DPmsg.getLength());
				this.jTexto.append( this.message + "\n");
				serverSocket.close();
			}

		} catch(Exception exc) {
			exc.printStackTrace();
		}
	}

}
